﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace UIT_CV_Management
{
    public class BaoCaoCanBo
    {
        public String ID;
        public String MaBaoCaoCanBo;
        public String TenBaoCao;

        public BaoCaoCanBo(String _ID)
        {

            QueryResult result = Database.shareDatabase.Query("SELECT * FROM BaoCaoCanBo " +
                " WHERE ID = '" + _ID + "'");
            if (result.ReturnCode == 200)
            {
                DataRow row = result.Table.Rows[0];
                ID = _ID;
                MaBaoCaoCanBo = row["MaBaoCaoCanBo"].ToString();
                TenBaoCao = row["TenBaoCao"].ToString();
            }
            else
                MessageBox.Show(result.Error);
        }

        public BaoCaoCanBo(String _MaBaoCaoBanBo, String _TenBaoCao)
        {
            ID = Database.getID("BaoCaoBanBo", "ID");
            MaBaoCaoCanBo = _MaBaoCaoBanBo;
            TenBaoCao = _TenBaoCao;
        }

        public Boolean Save()
        {
            QueryResult result;
            if (Database.isDupplicate("BaoCaoCanBo", "ID", ID)) { 
                //Update
                result = Database.shareDatabase.Query("UPDATE BaoCaoCanBo SET " + 
                    "MaBaoCaoCanBo = '" + MaBaoCaoCanBo + "', " +
                    "TenBaoCao = '" + TenBaoCao + "' " +
                    "WHERE ID = '" + ID + "'");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
            else {
                //Insert
                result = Database.shareDatabase.Query("INSERT INTO BaoCaoCanBo VALUES(" +
                    "'" + ID + "', " +
                    "'" + MaBaoCaoCanBo + "', " +
                    "'" + TenBaoCao + "')");
                if (result.ReturnCode == 200)
                    return true;
                else
                    return false;
            }
        }
        public Boolean Delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM BaoCaoCanBo " +
                "WHERE ID = '" + ID + "'");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }

        public static List<BaoCaoCanBo> getAllBaoCaoCanBo(String _MaBaoCaoCanBo)
        {
            QueryResult result = Database.shareDatabase.Query("SELECT * FROM BaoCaoCanBo " +
                "WHERE MaBaoCaoCanBo = '" + _MaBaoCaoCanBo + "'");
            if (result.ReturnCode == 200)
            {
                List<BaoCaoCanBo> _result = new List<BaoCaoCanBo>();
                foreach (DataRow dtr in result.Table.Rows)
                    _result.Add(new BaoCaoCanBo(dtr["ID"].ToString()));
                return _result;
            }
            else
                return null;
        }
    }
}
